package javavis.jip3d.functions;

import javavis.base.Function3DGroup;
import javavis.base.JIPException;
import javavis.base.JIPToolkit;
import javavis.base.ParamType;
import javavis.jip2d.base.bitmaps.JIPBmpByte;
import javavis.jip3d.geom.PointSR4K;
import javavis.jip3d.gui.Function3D;
import javavis.jip3d.gui.FunctionParam;
import javavis.jip3d.gui.ScreenData;
import javavis.jip3d.gui.ScreenOptions;
import javavis.jip3d.gui.dataobjects.SR4000Set3D;

public class FSR4kConfidence extends Function3D {

	public FSR4kConfidence() {
		name = "FSR4kConfidence";
		this.allowed_input = ScreenOptions.tSR4000SET3D;
		this.group = Function3DGroup.Image;

		FunctionParam p1 = new FunctionParam("Output", ParamType.FILE); 
		addParam(p1);
	}

	@Override
	public void proccessData(ScreenData scrData) throws JIPException {
		String completePath = paramValueString("Output");
		int cont, x, y, color;
		double []pixels;
		double maxValue = 0.0;
		JIPBmpByte confidence = new JIPBmpByte(SR4000Set3D.getNcolumns(), SR4000Set3D.getNrows());
		
		Object []elements = scrData.elements();
		PointSR4K point;
		
		pixels = new double[SR4000Set3D.getNcolumns() * SR4000Set3D.getNrows()];
		for(cont=0;cont<elements.length;cont++)
		{
			point = (PointSR4K)elements[cont];
			x = point.posx;
			y = point.posy;
			color = point.confidence;
			pixels[y * SR4000Set3D.getNcolumns() + x] = color; 
			if(color > maxValue) maxValue = color;
		}
		double auxConf=255.0/maxValue;
		for (cont=0; cont<elements.length; cont++) {
			pixels[cont] *= auxConf;
		}

		confidence.setAllPixels(pixels);
		
		//write into file
		if(completePath.substring(completePath.lastIndexOf('.')+1).compareTo("jip")!=0)
		{
			completePath += ".jip";
		}
		JIPToolkit.saveImageIntoFile(confidence, completePath);
	}

}
